﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.Model;
using QuanLyHocPhi.BLL;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class FormDangKyPhiMacDinh : Form
    {
        private NamHoc m_NamHoc;
        private HocKy m_HocKy;
        private List<LopGoc> m_LopGocList = new List<LopGoc>();
        private List<String> m_LopGocDaQuyDinhMucThuList = new List<string>();

        public FormDangKyPhiMacDinh(NamHoc namHoc, HocKy hocKy)
        {
            InitializeComponent();
            try
            {
                nudHocPhi.Value = Properties.Settings.Default.HocPhiMacDinh;
                nudLePhi.Value = Properties.Settings.Default.LePhiMacDinh;
            }
            catch
            {                
            }

            m_NamHoc = namHoc;
            m_HocKy = hocKy;
            lblTitle.Text = string.Format("Thiết lập mức thu học phí và lệ phí cho học kỳ {0} năm học {1}",
                hocKy.TenHocKy, namHoc.TenNamHoc);
            nudHocPhi.Focus();

            bsLopGoc.DataSource = m_LopGocList;
            gridLopGoc.ReadOnly = true;
            gridLopGoc.AutoGenerateColumns = false;
            gridLopGoc.AllowUserToAddRows = false;
            gridLopGoc.AllowUserToDeleteRows = false;
            gridLopGoc.DataSource = bsLopGoc;
            UpdateGuiBySelectedLopGoc();

            m_LopGocDaQuyDinhMucThuList = BizPhiMacDinh.GetListMaLopGocDaQuyDinhMucThu(m_HocKy.MaHocKy);
        }

        private void btnLop_BoSung_Click(object sender, EventArgs e)
        {
            // Lấy danh sách những lớp gốc đã đăng ký mức thu ở mã học kỳ này
            var lstExcludedMaLopGoc = new List<string>();
            foreach (var st in m_LopGocDaQuyDinhMucThuList)
                lstExcludedMaLopGoc.Add(st);
            if (bsLopGoc.DataSource != null)
            {
                List<LopGoc> lstLopGoc = bsLopGoc.DataSource as List<LopGoc>;
                foreach (var lopGoc in lstLopGoc)
                    lstExcludedMaLopGoc.Add(lopGoc.MaLopGoc);
            }

            var frm = new FormSelectLopGoc(lstExcludedMaLopGoc);
            frm.Size = this.Size;
            frm.Location = this.Location;
            frm.StartPosition = FormStartPosition.Manual;
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                bsLopGoc.SuspendBinding();
                var lstLopGoc = frm.SelectedLopGocList;
                m_LopGocList.AddRange(lstLopGoc);
                bsLopGoc.ResumeBinding();
                bsLopGoc.ResetBindings(false);
                gridLopGoc.AutoGenerateColumns = false;
                gridLopGoc.ReadOnly = true;
                gridLopGoc.DataSource = bsLopGoc;
            }
        }

        private void bsLopGoc_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedLopGoc();
        }

        private void UpdateGuiBySelectedLopGoc()
        {
            btnLop_Xoa.Enabled = bsLopGoc.Current != null;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (bsLopGoc.DataSource == null)
                return;
            if (bsLopGoc.Count == 0)
                return;

            try
            {
                // Thực hiện thiết lập mức thu
                var hocPhi = Convert.ToInt32(nudHocPhi.Value);
                var lePhi = Convert.ToInt32(nudLePhi.Value);
                var lstLopGoc = bsLopGoc.DataSource as List<LopGoc>;
                if (lstLopGoc == null)
                    throw new Exception("Danh sách lớp không hợp lệ");

                var lstMaLopGoc = lstLopGoc.Select(x => x.MaLopGoc).ToList();
                BizPhiMacDinh.SetPhiMacDinh(m_HocKy, lstMaLopGoc, hocPhi, lePhi);

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch (Exception exc)
            {
                DialogUtils.ShowError(exc.Message);
            }
        }

        private void gridLopGoc_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            try
            {
                if (gridLopGoc.DataSource == null || bsLopGoc.DataSource == null)
                    return;
                if (gridLopGoc.Rows.Count == 0)
                    return;
                var idx = colLopGoc_TenLoaiHinhDaoTao.Index;
                for (int i = 0; i < gridLopGoc.Rows.Count; i++)
                {
                    var row = gridLopGoc.Rows[i];
                    var lop = row.DataBoundItem as LopGoc;
                    if (lop == null)
                        continue;
                    var cell = row.Cells[idx];
                    cell.Value = lop.LoaiHinhDaoTao.TenLoaiHinh;
                }
            }
            catch (Exception exc)
            {
                DialogUtils.ShowError(exc.Message);
            }
        }

        private void btnLop_Xoa_Click(object sender, EventArgs e)
        {
            if (bsLopGoc.Current != null)
                bsLopGoc.RemoveCurrent();
        }
    }
}
